package com.cqndt.disaster.device.controller;

import java.util.Date;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import javax.servlet.http.HttpServletRequest;
import com.cqndt.disaster.device.util.Result;
import org.apache.tomcat.util.codec.binary.Base64;
import com.cqndt.disaster.device.vo.TabOrderLogVo;
import com.cqndt.disaster.device.domain.TabOrderLog;
import com.cqndt.disaster.device.service.ContrlService;
import com.cqndt.disaster.device.common.util.ContrlUtil;
import com.cqndt.disaster.device.service.TabOrderLogService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cqndt.disaster.device.common.util.NumberChangeUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import com.cqndt.disaster.device.common.exception.ContrlException;

/**
 * @author lgb
 * @date 2020-9-1
 * @return 智能报警器指令
 **/
@RestController
@RequestMapping("/api/v2/contrl")
public class ContrlController {
	
    @Autowired
    private TabOrderLogService tabOrderLogService;
    @Autowired
    private ContrlService contrlService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * @param request
     * @param platDeviceId 平台设备id
     * @param sn 设备sn
     * @param voiceType 声音类型
     * @param voiceTypeName 声音类型名称
     * @return 智能报警器指令
     **/
    @PostMapping(value="order")
    public Result order(HttpServletRequest request,String platDeviceId, String sn, String voiceType, String voiceTypeName) {
        String userId = request.getHeader("id");
        try {
            //保存发送日志
            TabOrderLog log = new TabOrderLog();
            log.setOperation("手机发送智能报警指令");
            log.setVoiceTypeName(voiceTypeName);
            log.setPlatType(1);
            log.setSn(sn);
            log.setVoiceType(Integer.valueOf(voiceType));
            log.setDeviceId(platDeviceId);
            log.setCreateDate(new Date());
            log.setUsername(redisTemplate.opsForValue().get("userName"+userId));
            log.setIp(redisTemplate.opsForValue().get("uuid"+userId));
            log.setTime(1l);
            tabOrderLogService.save(log);
            //申请变量
            String setShoudLight =  ContrlUtil.setAlarm(sn,Integer.parseInt(voiceType));//声光告警
            //发送请求onenet
            contrlService.postCommand(platDeviceId, Base64.encodeBase64String(NumberChangeUtil.stringToByte(setShoudLight)));
        } catch (ContrlException e) {
            return new Result().failure(-1,platDeviceId + e.getMessage());
        }
        //响应
        return new Result().success("发送成功");
    }
    /**
     * @param vo
     * @return 已发送指令列表
     **/
    @PostMapping(value="listOrderLog")
    public Result listOrderLog(TabOrderLogVo vo) {
        PageHelper.startPage(vo.getPage(),vo.getLimit());
        PageInfo<TabOrderLogVo> pageInfo = new PageInfo<TabOrderLogVo>(tabOrderLogService.listOrderLog(vo));
        Result result = new Result();
        result.setCount((int)pageInfo.getTotal());
        result.setData(pageInfo.getList());
        return result;
    }
}